package com.jpyl.jpMedicalTreatment.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import com.forxindu.kit.box.PreferenceKit;
import com.forxindu.kit.box.ViewKit;
import com.jpyl.jpMedicalTreatment.MedicalApplication;
import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.common.MedicalSettings;
import com.jpyl.jpMedicalTreatment.model.RxSpf_UserModel;
import com.jpyl.jpMedicalTreatment.model.helper.BottomNavigationViewHelper;
import com.jpyl.jpMedicalTreatment.view.activity.self.CertificationActivity;
import com.jpyl.jpMedicalTreatment.view.adapter.CommonFragmentPagerAdapter;
import com.jpyl.jpMedicalTreatment.view.fragment.common.GeneralAgentSelfFragment;
import com.jpyl.jpMedicalTreatment.view.fragment.common.HomeFragment;
import com.jpyl.jpMedicalTreatment.view.fragment.common.NewsFragment;
import com.jpyl.jpMedicalTreatment.view.fragment.common.ProductFragment;
import com.jpyl.jpMedicalTreatment.view.fragment.common.SelfFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 首页
 * Created by 521546564465 on 2018/11/5.
 */

public class HomeActivity extends BaseActivity {
    RxSpf_UserModel spfUserModel;

    @BindView(R.id.navigation)
    BottomNavigationView navigation;

    @BindView(R.id.home_viewpager)
    ViewPager viewPager;

    private CommonFragmentPagerAdapter pagerAdapter;
    public static Activity activity;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void afterCreate(Bundle savedInstanceState) {
        spfUserModel = RxSpf_UserModel.create(this);

        String cookieStr = PreferenceKit.getSharedPreference(MedicalApplication
                .applicationContext, MedicalSettings.MEMBER_COOKIE, "");
        Log.e("Activity", "======cookieStr======" + cookieStr);
        activity = this;
        setToolBarEnabled(false);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        //调用上面的类进行设置
        BottomNavigationViewHelper.disableShiftMode(navigation);
        pagerAdapter = new CommonFragmentPagerAdapter(getSupportFragmentManager());
        setFragment();

        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
                navigation.getMenu().getItem(arg0).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
            }
        });
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    viewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_dashboard:
                    viewPager.setCurrentItem(1);
                    return true;
                case R.id.navigation_notifications:
                    viewPager.setCurrentItem(2);
                    return true;
                case R.id.navigation_personal:
                    viewPager.setCurrentItem(3);
                    return true;
            }
            return false;
        }
    };

    private void setFragment() {
        List<Fragment> fragmentList = new ArrayList<>();
        int userFlag = getIntent().getIntExtra
                (MedicalSettings.STRUSERTYPE, -1);
        String userId = spfUserModel.userId().get();
        HomeFragment homeFragment = HomeFragment.getInstance(userFlag, userId);
        ProductFragment productFragment = new ProductFragment(this);
        NewsFragment shoppingTrolleyFragment = new NewsFragment(this);
        SelfFragment selfFragment = new SelfFragment(this);
        GeneralAgentSelfFragment generalAgentSelfFragment = new GeneralAgentSelfFragment(this);

        fragmentList.add(homeFragment);
        fragmentList.add(productFragment);
        fragmentList.add(shoppingTrolleyFragment);

        //根据用户身份判断，显示我的界面
        if (userFlag == MedicalSettings.USERTYPE_PATIENT) {
            //（患者身份）
            fragmentList.add(selfFragment);

        } else {
            // （总代理,代理,医生身份）
            fragmentList.add(generalAgentSelfFragment);
        }
        pagerAdapter.setFragmentList(fragmentList);
        pagerAdapter.notifyDataSetChanged();
    }

    // 用来计算返回键的点击间隔时间
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ViewKit.showToast(HomeActivity.this, "再按一次将退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
